﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using Yitter.IdGenerator;

namespace CoreShop.Services
{
    /// <summary>
    ///  接口实现
    /// </summary>
    public class corecmsfoodorderdetailServices : Repository<corecmsfoodorderdetail>, IcorecmsfoodorderdetailServices
    {
        public bool InsertAsync(corecmsfoodorderdetail entity)
        {
            return base.Change<corecmsfoodorderdetail>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodorderdetail> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodorderdetail entity)
        {
            return base.Change<corecmsfoodorderdetail>().Update(a => new corecmsfoodorderdetail()
            {
                orderId = entity.orderId,
                rowId = entity.rowId,
                foodId = entity.foodId,
                foodSpecId = entity.foodSpecId,
                convert = entity.convert,
                quantity = entity.quantity,
                price = entity.price,
                realPrice = entity.realPrice,
                discount = entity.discount,
                amount = entity.amount,
                realAmount = entity.realAmount,
                taskAmount = entity.taskAmount,
                feedAmount = entity.feedAmount,
                costPrice = entity.costPrice,
                itemType = entity.itemType,
                remark = entity.remark
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodorderdetail> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodorderdetail>().Update(a => new corecmsfoodorderdetail()
                {
                    orderId = item.orderId,
                    rowId = item.rowId,
                    foodId = item.foodId,
                    foodSpecId = item.foodSpecId,
                    convert = item.convert,
                    quantity = item.quantity,
                    price = item.price,
                    realPrice = item.realPrice,
                    discount = item.discount,
                    amount = item.amount,
                    realAmount = item.realAmount,
                    taskAmount = item.taskAmount,
                    feedAmount = item.feedAmount,
                    costPrice = item.costPrice,
                    itemType = item.itemType,
                    remark = item.remark
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodorderdetail>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodorderdetail>().DeleteById(id);
            }
            return returnKey;
        }
        public List<corecmsfoodorderdetail> SelectByOrderId(List<long> orderIds)
        {
            List<corecmsfoodorderdetail> lst = base.AsQueryable().Where(p => orderIds.Contains(p.orderId)).ToList();
            return lst;
        }

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodorderdetail> GetCaChe()
        {
            return base.GetList();
        }

        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodorderdetail> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodorderdetail> custos = new List<corecmsfoodorderdetail>();
            var where = Expressionable.Create<corecmsfoodorderdetail>();

            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        /// <summary>
        /// 前台商品销售明细统计(传后台报表库数据)
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodsaledetail> QueryUpdataDetail(long orderId, sysuser sysUser)
        {
            var list1 = new corecmsfoodorderdetailServices().AsQueryable()
               .LeftJoin<corecmsfoodordermaster>((a, b) => a.orderId == b.orderId)
               .LeftJoin<corecmsfoodcategory>((a, b, t) => t.id == a.foodTypeId)
               .LeftJoin<corecmsfooddesk>((a, b, t, s) => b.deskId == s.id)
               .Where((a, b, t, s) => a.orderId == orderId)
               .Select((a, b, t, s) => new corecmsfoodsaledetail
               {
                   id = a.id,
                   orderId = b.orderId,
                   orderDate = b.orderDate,
                   orderNo = b.orderNo,
                   corpId = b.corpId,
                   storeId = b.storeId,
                   storeName = sysUser.storeName,
                   foodId = a.foodId,
                   foodSpecId = a.foodSpecId,
                   foodCode = a.number,
                   foodName = a.name,
                   specName = a.specName,
                   foodCategoryId = a.foodTypeId,
                   foodCategoryName = t.name ?? "",
                   price = a.price,
                   quantity = a.quantity,
                   amount = a.amount,
                   discount = a.discount,
                   discountAmount = a.amount - a.realAmount,
                   realAmount = a.realAmount,
                   departId = (long)t.departId,
                   departName = "",
                   ordertype = b.ordertype,
                   dataType = a.dataType,
                   deskId = b.deskId,
                   deskName = s.name,
                   remark = a.remark,
                   detailId = a.id,
                   realPrice = a.realPrice,
                   taskAmount = a.taskAmount,
                   feedAmount = a.feedAmount,
                   reduceAmount = a.reduceAmount,
                   allAmount = a.allAmount,
                   costPrice = (decimal)a.costPrice,
                   setPrice = a.setPrice,
                   itemType = a.itemType,
                   billerId=a.billerId,
               }).With(SqlWith.NoLock).ToList();
            return list1;
        }

    }
}